/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.dusty.fs.rm;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import ru.dusty.fs.filesystem.utils.Utils;

/**
 *
 * @author Dusty
 */
public class RmFs {
    
    private static final String NAME = "rm";
    
    //private BlocksMap map;
    
    public int rm(String[] args) {
        if (args.length < 1) {
            System.out.println(NAME + ": missing operand after \"" + NAME + "\"");
            System.out.println(NAME + ": use \"" + NAME + " root file\"");
            return 1;
        }
        
        String root = args[0];
        String path = (args.length > 1) ? args[1] : "";

        List<String> paths = Utils.parsePath(path);
        
        try {
            if (!Utils.isFsFormatted(root)) {
                System.out.println(NAME + ": File system error.");
                System.out.println(NAME + ": Use the command \"format root\"");
                return 5;
            }
            
//            map = new BlocksMap(root, Utils.getBlocksCount(root));
//            map.initMap();
            
            Utils.remove(root, /*map,*/ paths);
        } catch (FileNotFoundException ex) {
            System.out.println(NAME + ": Could not find the file block.");
            System.out.println(NAME + ": " + ex.getMessage());
            return 2;
        } catch (IOException ex) {
            System.out.println(NAME + ": Error occurred while reading the file block.");
            System.out.println(NAME + ": " + ex.getMessage());
            return 3;
        }

        return 0;
    }
    
}
